package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.RechargeDto;
import io.geekidea.boot.shop.entity.Recharge;
import io.geekidea.boot.shop.mapper.RechargeMapper;
import io.geekidea.boot.shop.query.app.AppRechargeQuery;
import io.geekidea.boot.shop.query.web.RechargeQuery;
import io.geekidea.boot.shop.service.base.api.RechargeService;
import io.geekidea.boot.shop.vo.app.AppRechargeVo;
import io.geekidea.boot.shop.vo.web.RechargeVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class RechargeServiceImpl extends ServiceImpl<RechargeMapper, Recharge> implements RechargeService {

    @Autowired
    private RechargeMapper rechargeMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addRecharge(RechargeDto dto) {
        Recharge recharge = new Recharge();
        BeanUtils.copyProperties(dto, recharge);
        return save(recharge);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateRecharge(RechargeDto dto) {
        Integer id = dto.getId();
        Recharge recharge = getById(id);
        if (recharge == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, recharge);
        return updateById(recharge);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteRecharge(Integer id) {
        return removeById(id);
    }

    @Override
    public RechargeVo getRechargeById(Integer id) {
        return rechargeMapper.getRechargeById(id);
    }

    @Override
    public Paging<RechargeVo> getRechargePage(RechargeQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<RechargeVo> list = rechargeMapper.getRechargePage(query);
        Paging<RechargeVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppRechargeVo getAppRechargeById(Integer id) {
        return rechargeMapper.getAppRechargeById(id);
    }

    @Override
    public Paging<AppRechargeVo> getAppRechargePage(AppRechargeQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppRechargeVo> list = rechargeMapper.getAppRechargePage(query);
        Paging<AppRechargeVo> paging = new Paging<>(list);
        return paging;
    }

}
